<?php
declare (strict_types = 1);

namespace app\move\controller;

use app\BaseController;
use app\homl\model\User;
use app\move\services\RegServices;
use think\facade\Cache;
use think\Request;

class Login extends BaseController
{
    public function registerView(){
        return view('register');
    }
    public function smsend(Request $request){
//        return 111;
        $prams=$request->post();
        try {
            $code=mt_rand(1000,9999);
            $content='验证码:'.$code;

//            //缓存的验证码
            $smsCode='sms_code'.$prams['phone'];
//            //发送的频率
            $smsDayTime='sms_day_time'.$prams['phone'];
            $qsmsDayTime=Cache::get($smsDayTime);
            if($qsmsDayTime){
                if(time()-$qsmsDayTime<10){
                    $endTime=60-(time()-$qsmsDayTime);
                    abort(102,'你的发送频率过于频繁，请'.$endTime.'稍后再试');
                }
            }

            //发送的次数
            $smsdayCount='sms_day_count'.$prams['phone'].'_'.date('ymd');
            $qsmsdayCount=Cache::get($smsdayCount);
            if($qsmsdayCount && $qsmsdayCount>2){
                abort(103,'验证码超出一天的发送次数');
            }

            $res=RegServices::send($prams['phone'],$content,true);
            if($res['status']==0){
//                //缓存验证码
                Cache::set($smsCode,$code,600);
//                //验证码是否频繁
                Cache::set($smsDayTime,time(),30);
//                //自增
                Cache::inc($smsdayCount);
//                return $res;
            }else{
                abort(101,$res['msg']);
            }
            return $res;
        }catch (\Exception $e){
            return ['status'=>1,'msg'=>$e->getMessage()];
        }
    }

    public function register(Request $request){
        if($request->isPost()){
            $prams=$request->post();
            try {
                $this->validate($prams,[
                    'phone'=>'require|unique:user',
                    'code'=>'require',
                    'password'=>'require',
                    'repassword'=>'require|confirm:password',
                ],[
                    'repassword.confirm'=>'俩次密码不一致',
                    'code.require'=>'验证码不能为空',
                    'phone.unique'=>'手机号已经注册，请不要重复注册'
                ]);
                $smsCode='sms_code'.$prams['phone'];
                $qsmsCode=Cache::get($smsCode);
                if(!$qsmsCode){
                    abort(104,'请输入验证码');
                }
                //验证码缓存中取出的验证码是否与输入的验证码相同
                if($qsmsCode != $prams['code']){
                    abort(105,'验证码输入错误');
                }
                $smsCode='sms_code'.$prams['phone'];
                $smsDayTime='sms_day_time'.$prams['phone'];
                $data=[
                    'phone'=>$prams['phone'],
                    'username'=>$prams['phone'],
                    'password'=>md5($prams['password']),
                ];
//                dd($data);
                $res=User::create($data);
                if($res){
                    Cache::delete($smsCode);
                    Cache::delete($smsDayTime);
                    return ['status'=>0,'msg'=>'注册成功，请重新登录'];
                }else{
                    return ['status'=>1,'msg'=>'注册失败'];
                }
            }catch (\Exception $e){
                return ['status'=>1,'msg'=>$e->getMessage()];
            }

        }
    }
    public function login(Request $request){
        if($request->isPost()){
            $data=$request->post();
//            dd($data);

            try {
                $this->validate($data,[
                   'username'=>'require',
                   'password'=>'require',
                ],[
                    'username.require'=>'用户名错误',
                    'password.require'=>'密码错误',
                ]);
                $username=User::where('username',$data['username'])->find();
                if(!$username){
                    abort(106,'用户名错误');
                }
                $password=md5($data['password']);
                if($password != $username['password']){
                   abort(107,'密码错误');
                }
                //保持登陆状态
                session('userInfo',$username->toArray());
                //持久化登陆
                if(isset($data['ck']) && $data['ck']==1){
                    cookie('uid',$username['id'],7*24*3600);
                }
                return ['status'=>0,'登录成功'];
            }catch (\Exception $e){
                return ['status'=>1,'msg'=>$e->getMessage()];
            }
        }
        return view('login');
    }
    public function logout(){
        session('userInfo',null);
        cookie('uid',null);
        return redirect(url('login/login')->build());
    }
}

